<?php

define('Folder', 'files');
define('FALog', 'FileAccess.log');

/**
 * Logs a message.
 * @param type $message 
 */
function falog($message) {
    error_log($message . "<\n", 3, FALog);
}

/**
 * Creates a file in the users folder.
 * @param int/string $user the user owning the file
 * @param string $filename the files name
 * @return bool Returns true on success or false on failure.
 */
function userCreateFile($user, $filename) {
    if (is_string($user))
        $username = $user;

    $user = getUserId($user);

    if (!insertNewFile($filename, $user))
        return false;

    $path = $username . '\\' . $filename;

    ;
    if (($fileHandle = fopen($path, 'w')) == null) {
        falog("Couldn't open file >" . $filename .
                "<, username >" . $username);
        return false;
    }

    return true;
}

/**
 * Removes a file from the DB and FS.
 * @param int $fileid The file id.
 * @return bool Returns true on success or false on failure.
 */
function userRemoveFile($fileid) {
    list ($idCreator, $filename) = getFileProperties($fileid);

    if (!removeFile($fileid)) {
        falog("File remove failed. filename '" . $filename .
                "', userid '" . $idCreator .
                "', fileid '" . $fileid);
        return false;
    }

    $username = getUserName($idCreator);
    $path = $username . '\\' . $filename;
    if (!unlink($path)) {
        falog("File unlink failed. filename '" . $filename .
                "', username '" . $username);
        return false;
    }

    return true;
}

?>
